import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.List;
import java.util.Map;

/**
 * @author: Themberfue
 * @date: 2025/4/5 11:23
 * @description:
 */
public class RedisZset {
    public static void main(String[] args) {
        try (JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
             Jedis jedis = jedisPool.getResource()) {
            zaddAndZrange(jedis);
        }
    }

    private static void zscoreAndZrank(Jedis jedis) {
        jedis.flushAll();

        Map<String, Double> ms = Map.of("zhangsan", 10.0, "lishi", 20.0, "wangwu", 30.0);
        jedis.zadd("k1", ms);

        Double zscoreRes = jedis.zscore("k1", "zhangsan");
        System.out.println(zscoreRes);

        Long zrankRes = jedis.zrank("k1", "lishi");
        System.out.println(zrankRes);
    }

    private static void zcardAndZrem(Jedis jedis) {
        jedis.flushAll();

        Map<String, Double> ms = Map.of("zhangsan", 10.0, "lishi", 20.0, "wangwu", 30.0);
        jedis.zadd("k1", ms);

        long zcardRes = jedis.zcard("k1");
        System.out.println(zcardRes);

        long zremRes = jedis.zrem("k1", "zhangsan", "lishi", "zhaoliu");
        System.out.println(zremRes);
    }

    private static void zaddAndZrange(Jedis jedis) {
        jedis.flushAll();

        jedis.zadd("k1", 10.0, "zhangsan");
        Map<String, Double> ms = Map.of("lishi", 20.0, "wangwu", 30.0);
        jedis.zadd("k1", ms);

        List<String> zrangeRes = jedis.zrange("k1", 0, -1);
        System.out.println(zrangeRes);

        List<Tuple> membersWithScores = jedis.zrangeWithScores("k1", 0, -1);
        System.out.println(membersWithScores);
        System.out.println(membersWithScores.get(0).getElement());
        System.out.println(membersWithScores.get(0).getScore());
    }
}
